 //
//  CTDialKeyboard.m
//  CT
//
//  Created by liufanwei on 15/4/12.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTDialKeyboard.h"
#import "CommonMicro.h"

@interface CTDialKeyboard()<UITextFieldDelegate>{
    
}

@property (weak, nonatomic) IBOutlet UIButton *BTN1;
@property (weak, nonatomic) IBOutlet UIButton *BTN2;
@property (weak, nonatomic) IBOutlet UIButton *BTN3;
@property (weak, nonatomic) IBOutlet UIButton *BTN4;
@property (weak, nonatomic) IBOutlet UIButton *BTN5;
@property (weak, nonatomic) IBOutlet UIButton *BTN6;
@property (weak, nonatomic) IBOutlet UIButton *BTN7;
@property (weak, nonatomic) IBOutlet UIButton *BTN8;
@property (weak, nonatomic) IBOutlet UIButton *BTN9;
@property (weak, nonatomic) IBOutlet UIButton *BTN10;
@property (weak, nonatomic) IBOutlet UIButton *BTN11;
@property (weak, nonatomic) IBOutlet UIButton *BTN12;
@property (weak, nonatomic) IBOutlet UITextField *TextField;

@end

@implementation CTDialKeyboard
@synthesize delegate = _delegate;


-(void)awakeFromNib{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [_BTN1 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN2 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN3 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN4 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN5 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN6 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN7 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN8 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN9 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN10 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN11 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    [_BTN12 addTarget:self action:@selector(onNumberClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _TextField.clipsToBounds = YES;
    _TextField.layer.borderColor = kRGB16(0x5DCDFA).CGColor;
    _TextField.layer.borderWidth = 2.0f;
    _TextField.delegate = self;
}

-(void)onNumberClick:(UIButton*)sender{
    
    NSString * str = _TextField.text;
    NSInteger tag = sender.tag;
    NSString * subStr;
    if (tag == 10) {
        subStr = @"0";
    }else if (tag == 11){
        subStr = @"*";
    }else if (tag == 12){
        subStr = @"#";
    }else{
        subStr = [NSString stringWithFormat:@"%ld",tag];
    }
    str = [NSString stringWithFormat:@"%@%@",str,subStr];
    _TextField.text = str;
    if (_delegate) {
        [_delegate onTextChanged:str];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}

- (IBAction)onCallClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationHidKeyboard object:Nil];
    if (_delegate) {
        [_delegate onCallClicked:_TextField.text];
    }
}


- (IBAction)onSaveClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationHidKeyboard object:Nil];
    if (_delegate) {
        [_delegate onSaveClicked:_TextField.text];
    }
}

- (IBAction)onCancelClick:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationHidKeyboard object:Nil];
}

- (IBAction)onDeleClick:(id)sender {
     NSString * str = _TextField.text;
    if (str.length == 0) {
        return;
    }
    str = [str substringToIndex:str.length-1];
    _TextField.text = str;
    if (_delegate) {
        [_delegate onTextChanged:str];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
